$(function(){
	$("#publishBtn").click(publish);
});

function showHintModal({content, closeCallback}) {
    $("#hintBody").text(content);
    $("#hintModal").modal("show");
    setTimeout(function(){
        $("#hintModal").modal("hide");
        if (closeCallback) {
            closeCallback();
        }
    }, 2000);
}

function publish() {
    let title = $("#recipient-name").val();
    let content = $("#message-text").val();

    $.post(
        CONTEXT_PATH + "/discuss/publish",
        {title, content},
        function(data) {
            if (data.code == 0) {
                $("#publishModal").modal("hide");
                showHintModal({
                    content: data.msg,
                    closeCallback: () => window.location.reload(),
                });
            } else {
                showHintModal({
                    content: data.msg,
                });
            }
        }
    );
}